package com.xbongbong.paas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字段类型枚举类
 * @author: wufeng
 * @date: 2018/8/10 9:54
 * @desrcption: 字段类型枚举类
 */
public enum SubFieldTypeEnum {

    /**
     * 文本
     */
    TEXT(1, "text", "basic", "enum.subFieldType.description.text", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    TEXTAREA(7, "textarea", "basic", "enum.subFieldType.description.textarea", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    NUM(2, "num", "basic", "enum.subFieldType.description.num", SubEsAliasEnum.NUM.getEsAlias(), 1, SubEsAliasEnum.NUM.getMax()),


    DATETIME(4, "datetime", "basic", "enum.subFieldType.description.datetime", SubEsAliasEnum.DATE.getEsAlias(), 1, SubEsAliasEnum.DATE.getMax()),


    RADIOGROUP(10000, "radiogroup", "basic", "enum.subFieldType.description.radiogroup", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    CHECKBOXGROUP(9, "checkboxgroup", "basic", "enum.subFieldType.description.checkboxgroup", SubEsAliasEnum.ARRAY.getEsAlias(), 1, SubEsAliasEnum.ARRAY.getMax()),


    COMBO(3, "combo", "basic", "enum.subFieldType.description.combo", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    COMBOCHECK(10001, "combocheck", "basic", "enum.subFieldType.description.combocheck", SubEsAliasEnum.ARRAY.getEsAlias(), 1, SubEsAliasEnum.ARRAY.getMax()),


    ADDRESS(12, "address", "basic", "enum.subFieldType.description.address", SubEsAliasEnum.ADDRESS.getEsAlias(), 1, SubEsAliasEnum.ADDRESS.getMax()),


    LOCATION(10003, "location", "basic", "enum.subFieldType.description.location", SubEsAliasEnum.GEO.getEsAlias(), 1, SubEsAliasEnum.GEO.getMax()),


    IMAGE(6, "image", "basic", "enum.subFieldType.description.image", SubEsAliasEnum.FILE.getEsAlias(), 1, SubEsAliasEnum.FILE.getMax()),


    UPLOAD(8, "upload", "basic", "enum.subFieldType.description.upload", SubEsAliasEnum.FILE.getEsAlias(), 1, SubEsAliasEnum.FILE.getMax()),


    MEMO(10004, "memo", "basic", "enum.subFieldType.description.memo", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    HREF(10005, "href", "basic", "enum.subFieldType.description.href", SubEsAliasEnum.FILE.getEsAlias(), 1, SubEsAliasEnum.FILE.getMax()),


    LINKQUERY(10007, "linkquery", "complex", "enum.subFieldType.description.linkquery", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    LINKDATA(10008, "linkdata", "complex", "enum.subFieldType.description.linkdata", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    USER(10009, "user", "complex", "enum.subFieldType.description.user", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    USERGROUP(10010, "usergroup", "complex", "enum.subFieldType.description.usergroup", SubEsAliasEnum.ARRAY.getEsAlias(), 1, SubEsAliasEnum.ARRAY.getMax()),


    DEPT(10011, "dept", "complex", "enum.subFieldType.description.dept", SubEsAliasEnum.TEXT.getEsAlias(), 1, SubEsAliasEnum.TEXT.getMax()),


    DEPTGROUP(10012, "deptgroup", "complex", "enum.subFieldType.description.deptgroup", SubEsAliasEnum.ARRAY.getEsAlias(), 1, SubEsAliasEnum.ARRAY.getMax());


    /**
     * 字段类型
     */
    private Integer type;
    /**
     * 字段类型英文
     */
    private String alias;
    /**
     * 字段所属
     */
    private String belong;

    /**
     * 字段描述
     */
    private String description;

    /**
     * es索引前缀
     */
    private String esalias;

    /**
     * 索引最小值
     */
    private Integer min;

    /**
     * 索引最大值
     */
    private Integer max;

    SubFieldTypeEnum(Integer type, String alias, String belong, String description, String esalias, Integer min, Integer max){
        this.type = type;
        this.alias = alias;
        this.belong = belong;
        this.description = description;
        this.esalias = esalias;
        this.min = min;
        this.max = max;
    }

    public Integer getType() {
        return type;
    }

    public String getAlias() {
        return alias;
    }

    public String getBelong() {
        return belong;
    }

    public String getDescription() {
        return I18nMessageUtil.getMessage(description);
    }

    public String getEsalias() {
        return esalias;
    }

    public Integer getMin() {
        return min;
    }

    public Integer getMax() {
        return max;
    }

    public static List<Map<String, Object>> fieldTypeList(){
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list2 = new ArrayList<>();
        List<Map<String, Object>> list3 = new ArrayList<>();
        for (SubFieldTypeEnum e: values()) {
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if ("basic".equals(e.getBelong())) {
                map.put("alias", e.getAlias());
                map.put("type", e.getType());
                map.put("description", e.getDescription());
                map.put("belong", e.getBelong());
                map.put("esalias", e.getEsalias());
                map.put("min", e.getMin());
                map.put("max", e.getMax());
                list1.add(map);
            }
            if ("complex".equals(e.getBelong())) {
                map.put("alias", e.getAlias());
                map.put("type", e.getType());
                map.put("description", e.getDescription());
                map.put("belong", e.getBelong());
                map.put("esalias", e.getEsalias());
                map.put("min", e.getMin());
                map.put("max", e.getMax());
                list2.add(map);
            }
            if ("system".equals(e.getBelong())) {
                map.put("alias", e.getAlias());
                map.put("type", e.getType());
                map.put("description", e.getDescription());
                map.put("belong", e.getBelong());
                map.put("esalias", e.getEsalias());
                map.put("min", e.getMin());
                map.put("max", e.getMax());
                list3.add(map);
            }
        }
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("className", "basic");
        map.put("fieldTypes", list1);
        list.add(map);
        map.put("className", "complex");
        map.put("fieldTypes", list2);
        list.add(map);
        map.put("className", "system");
        map.put("fieldTypes", list3);
        list.add(map);
        return list;
    }

    public static SubFieldTypeEnum getFieldTypeEnum(Integer type) {
        for (SubFieldTypeEnum e: values()) {
            if(type.equals(e.getType())){
                return e;
            }
        }
        return null;
    }
    public static SubFieldTypeEnum getFieldTypeEnumByAlias(String alias) {
        for (SubFieldTypeEnum e: values()) {
            if(alias.equals(e.getAlias())){
                return e;
            }
        }
        return null;
    }
}
